/**
 * 
 */
package com.dingding.order.core.orderlist.biz.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.http.HttpStatus;
import org.springframework.stereotype.Component;

import com.dingding.common.core.base.dto.AbstractResponse;
import com.dingding.common.core.base.dto.ResponseEntity;
import com.dingding.common.core.beans.UserCoupon;
import com.dingding.facade.user.service.IUserInfoFacade;
import com.dingding.order.core.orderlist.biz.IGetNearbyOrderListService;
import com.dingding.order.core.orderlist.dao.IGetNearbyOrderListDao;
import com.dingding.order.dto.orderlist.NearbyOrderList;
import com.dingding.order.dto.orderlist.ReqGetNearbyOrderListDTO;
import com.dingding.order.dto.orderlist.ResCoupon;
import com.dingding.order.dto.orderlist.ResGetNearbyOrderList;

/**
 * @ClassName: IGetNearOrderList
 * @Description:获取附近订单列表接口
 * @author zhouxuan
 * @date 2016年6月4日 下午2:52:14
 * 
 */
@Component("getNearOrderList")
public class GetNearbyOrderListServiceImpl extends AbstractResponse<ResGetNearbyOrderList, ReqGetNearbyOrderListDTO>
		implements IGetNearbyOrderListService {
	@Resource(name = "userInfoService")
	private IUserInfoFacade userInfoFacade;

	@Resource(name = "getNearbyOrderListDao")
	private IGetNearbyOrderListDao getNearbyOrderListDao;

	/**
	 * @Title: getNearOrderList
	 * @Description:获取附近订单列表
	 * @param reqGetNearbyOrderListDTO
	 * @author zhouxuan
	 * @version V2.2.0
	 */
	@Override
	public String getNearOrderList(String requestData) {
		// 将对象转换为请求bean
		ReqGetNearbyOrderListDTO reqGetNearbyOrderListDTO = super.parseRequestJson(requestData);
		ResponseEntity<ResGetNearbyOrderList> response = new ResponseEntity<ResGetNearbyOrderList>();

		// 1.验证请求参数
		if (!validateParam(response, reqGetNearbyOrderListDTO)) {
			return super.responseBeanToJson(response);
		}
		// 2.获取优惠券信息
		ResCoupon resCoupon = this.getUserCoupon(reqGetNearbyOrderListDTO);
		// 3.获取附近订单列表
		List<NearbyOrderList> nearbyOrderLists = getNearbyOrderList(reqGetNearbyOrderListDTO, resCoupon);
		// 4.拼装响应
		assemblyContent(response, resCoupon, nearbyOrderLists);
		return super.responseBeanToJson(response);
	}

	/**
	 * @Title: getUserCoupon
	 * @Description:获取用户优惠券
	 * @author zhouxuan
	 * @version V2.2.0
	 */
	private ResCoupon getUserCoupon(ReqGetNearbyOrderListDTO reqGetNearbyOrderListDTO) {
		ResCoupon resCoupon = null;
		// 司机的情况下，返回一个空的优惠券
		if (reqGetNearbyOrderListDTO.getUserType() == 1) {
			resCoupon = new ResCoupon("", 0);
		} else {
			// TODO 获取乘客的优惠券。
			List<UserCoupon> couponLists = this.userInfoFacade.getUserCouponList(reqGetNearbyOrderListDTO.getUserId());
			if (couponLists != null && couponLists.size() > 0) {

				UserCoupon temp = couponLists.get(0);

				for (UserCoupon userCoupon : couponLists) {
					if (temp.getCouponMoney().compareTo(userCoupon.getCouponMoney()) > 0) {
						temp = userCoupon;
					}
				}

				resCoupon = new ResCoupon(temp.getUcId(), temp.getCouponMoney());
			}
		}
		return resCoupon;
	}

	/**
	 * @Title: assemblyContent
	 * @Description:拼装响应给客户端
	 * @author zhouxuan
	 * @version V2.2.0
	 */
	private ResGetNearbyOrderList assemblyContent(ResponseEntity<ResGetNearbyOrderList> response, ResCoupon resCoupon,
			List<NearbyOrderList> nearbyOrderLists) {
		ResGetNearbyOrderList resGetNearbyOrderList = new ResGetNearbyOrderList();
		resGetNearbyOrderList.setCoupon(resCoupon); // 优惠券
		resGetNearbyOrderList.setNearbyOrderList(nearbyOrderLists); // 订单list
		response.setCode(HttpStatus.SC_OK);
		response.setResponseBody(resGetNearbyOrderList);
		return resGetNearbyOrderList;
	}

	/**
	 * @Title: getNearbyOrderList
	 * @Description:获取附近订单列表
	 * @param reqGetNearbyOrderListDTO
	 * @author zhouxuan
	 * @version V2.2.0
	 */
	private List<NearbyOrderList> getNearbyOrderList(ReqGetNearbyOrderListDTO reqGetNearbyOrderListDTO,
			ResCoupon resCoupon) {

		List<NearbyOrderList> getNearbyOrderList = null;
		// 判断请求用户是乘客还是司机
		if (reqGetNearbyOrderListDTO.getUserType() == 2) {
			// TODO 用户是乘客，从缓存中获取附近订单列表

		} else {
			// TODO 用户是司机，从缓存中获得附近订单列表
		}
		// 从缓存中获取订单列表失败，从数据库中获取订单列表
		if (getNearbyOrderList == null) {
			getNearbyOrderList = this.getNearbyOrderListDao.getNearbyOrderList(reqGetNearbyOrderListDTO, resCoupon);
		}
		return getNearbyOrderList;
	}
}
